package com.purchase.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.purchase.entity.InviteCode;
import com.purchase.mapper.InviteCodeMapper;
import com.purchase.service.InviteCodeService;
import com.purchase.util.InviteCodeUtil;
import com.purchase.util.LogInfo;
import com.purchase.util.RequestForm;
import com.purchase.util.ResponseForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Slf4j
@Service
public class InviteCodeServiceImpl implements InviteCodeService {

    @Autowired
    private InviteCodeMapper inviteCodeMapper;

    @Override
    public ResponseForm createCode(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        try {
            //创建者
            String wxUid = (String) paramMap.get("wxUid");
            //生成个数
            String num = (String) paramMap.get("num");
            //邀请码级别，0：父级，1：子级
            String createClass = (String) paramMap.get("class");
            if (!StringUtils.isNotBlank(wxUid)
                    || !StringUtils.isNotBlank(num)
                    || !StringUtils.isNotBlank(createClass)) {
                result.setStatus(false);
                result.setMessage(LogInfo.PARAM_ERROR);
                log.error(LogInfo.PARAM_ERROR);
                return result;
            }

            ArrayList<String> codes = this.generateCode(wxUid, num, createClass);
            if (codes == null) {
                result.setStatus(false);
                result.setMessage("该用户:" + wxUid + "未关联邀请码，无法创建下级邀请码");
                log.error("该用户:" + wxUid + "未关联邀请码，无法创建下级邀请码");
                return result;
            }

            result.setData(codes);
        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("生成邀请码error", e);
        }

        return result;
    }

    @Override
    public ResponseForm getCode(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        try {
            String wxUid = (String) paramMap.get("wxUid");
            if (!StringUtils.isNotBlank(wxUid)) {
                result.setStatus(false);
                result.setMessage(LogInfo.PARAM_ERROR);
                log.error(LogInfo.PARAM_ERROR);
                return result;
            }

            com.purchase.util.Page.parsePage(paramMap);
            Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
            List<InviteCode> inviteCodes = inviteCodeMapper.getInviteCodeList(wxUid);
            result.setData(inviteCodes);
            result.setTotal((int) pageHelper.getTotal());
        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("查看邀请码error", e);
        }
        return result;
    }

    @Override
    public ArrayList<String> generateCode(String wxUid, String num, String createClass) {
        int createNum = Integer.parseInt(num);

        Integer parentId = 0;
        if ("0".equals(createClass)) {
            //如果创建父级邀请码，则 parentId=0
            parentId = 0;
        } else if ("1".equals(createClass)) {
            //如果创建子级邀请码，则 parentId=该用户所关联的inviteCodeId
            Example example = new Example(InviteCode.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("wxUid", wxUid);
            List<InviteCode> inviteCodes = inviteCodeMapper.selectByExample(example);
            if (inviteCodes.size() == 0) {
                return null;
            }
            parentId = inviteCodes.get(0).getId();
        }

        //生成邀请码
        ArrayList<InviteCode> inviteCodes = new ArrayList<>();
        ArrayList<String> codes = new ArrayList<>();
        //入库邀请码
        for (int i = 0; i < createNum; i++) {
            String code = InviteCodeUtil.generateCode(6);
            int countCode = inviteCodeMapper.getCountCode(code);
            if (countCode > 0) {
                createNum++;
                continue;
            }
            InviteCode inviteCode = new InviteCode();
            inviteCode.setInviteCode(code);
            inviteCode.setCreateWxUid(wxUid);
            inviteCode.setParentId(parentId);
            inviteCodes.add(inviteCode);
            codes.add(code);
        }
        inviteCodeMapper.insertCodeBatch(inviteCodes);
        return codes;
    }

    @Override
    public void useCode(String wxUid, String inviteCode) {
        inviteCodeMapper.disableInviteCode(wxUid, inviteCode);
    }

}
